import { defineStore } from 'pinia';

export const useDictStore = defineStore('dict', {
  state: () => ({
    userVip: { names: {}, consts: [] },
    payType: { names: {}, consts: [] },
    online: { names: {}, consts: [] },
    productType: { names: {}, consts: [] },
    payStatus: { names: {}, consts: [] },
    sysNormalDisable: { names: {}, consts: [] },
    sysUserSex: { names: {}, consts: [] },
  }),
  actions: {
    transformToObjectArray(data) {
      return Object.entries(data).map(([key, value]) => ({
        value: key,
        label: value,
      }));
    },
    setUserVip(data) {
      this.userVip.names = data;
      this.userVip.consts = this.transformToObjectArray(data);
    },
    setPayType(data) {
      this.payType.names = data;
      this.payType.consts = this.transformToObjectArray(data);
    },
    setOnline(data) {
      this.online.names = data;
      this.online.consts = this.transformToObjectArray(data);
    },
    setProductType(data) {
      this.productType.names = data;
      this.productType.consts = this.transformToObjectArray(data);
    },
    setPayStatus(data) {
      this.payStatus.names = data;
      this.payStatus.consts = this.transformToObjectArray(data);
    },
    setSysNormalDisable(data) {
      this.sysNormalDisable.names = data;
      this.sysNormalDisable.consts = this.transformToObjectArray(data);
    },
    setSysUserSex(data) {
      this.sysUserSex.names = data;
      this.sysUserSex.consts = this.transformToObjectArray(data);
    },
  },
  // 👇 持久化配置
  persist: {
    enabled: true,
    storage: localStorage,
    key: 'my_dict_store_key',
  },
});
